﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using V5_Common;
using System.Data;
using V5_EShop.Data;

namespace V5_EShop.Admin.Help {
    public partial class Admin_Help : AdminPage {
        protected void Page_Load(object sender, EventArgs e) {
            if (!this.IsPostBack) {
                string Action = InputHelper.SetString(Request["Action"]);
                this.Bind_DropDownList();
                switch (Action.ToLower().Trim()) {
                    case "edit":
                        this.MultiViewList.ActiveViewIndex = 1;
                        if (Session["ClassId"] != null) {
                            this.ddlClassID.SelectedIndex = this.ddlClassID.Items.IndexOf(
                                    this.ddlClassID.Items.FindByValue(Session["ClassId"].ToString())
                                );
                        }
                        break;
                    default:
                        Bind_DataList();
                        break;
                }
            }
        }
        private void Bind_DropDownList() {
            this.ddlClassID.DataSource = V5.Class.GetDropDownList("Tbl_HelpClass", 0, "作为一级分类");
            this.ddlClassID.DataTextField = "ClassTitle";
            this.ddlClassID.DataValueField = "ClassID";
            this.ddlClassID.DataBind();
        }

        private void Bind_DataList() {
            this.MultiViewList.ActiveViewIndex = 0;
            int RecordCount = 0;
            DataTable dt = V5.SQL.Query_Pager("*", "(Select B.ClassTitle,A.* From Tbl_Help A Left Join Tbl_HelpClass B On A.ClassId=B.ClassId) AA", "", "Id Desc", Pager.PageSize, Pager.CurrentPageIndex, ref RecordCount);
            this.Pager.RecordCount = RecordCount;
            this.gvList.DataSource = dt;
            this.gvList.DataBind();
        }

        private void Bind_DataEdit(int id) {
            this.MultiViewList.ActiveViewIndex = 1;
            ModelHelp model = new ModelHelp(p => p.ID == id);
            if (model != null) {
                this.txtHelpTitle.Text = model.Title;
                this.ddlClassID.SelectedIndex = this.ddlClassID.Items.IndexOf(this.ddlClassID.Items.FindByValue(model.ClassID.ToString()));
                this.txtContent.Text = model.Content;
                this.hfID.Value = model.ID.ToString();

                this.lblTitle.Text = "修改帮助";
                this.btnSave.Text = "修改保存";
            }
        }

        private void Bind_Delete(int id) {
            new ModelHelp(p => p.ID == id).Delete();
            this.Bind_DataList();
        }

        protected void Pager_PageChanged(object sender, EventArgs e) {
            this.Bind_DataList();
        }

        protected void btnSave_Click(object sender, EventArgs e) {
            int id = InputHelper.SetNumber(this.hfID.Value);
            string title = this.txtHelpTitle.Text;
            int classid = InputHelper.SetNumber(this.ddlClassID.SelectedValue);
            string ClassName = this.ddlClassID.Items.FindByValue(this.ddlClassID.SelectedValue).Text;
            string content = this.txtContent.Text;
            ModelHelp model = new ModelHelp();
            if (id > 0) {
                model = new ModelHelp(p => p.ID == id);
            }
            model.Title = title;
            model.ClassID = classid;
            model.Content = content;
            model.AddDateTime = DateTime.Now;
            model.Hits = 0;
            if (id == 0) {

                Session["ParentId"] = classid;

                model.Add();
            }
            else if (id > 0) {
                model.ID = id;
                model.Update();
            }
            this.Bind_DataList();
        }

        protected void gvList_RowCommand(object sender, GridViewCommandEventArgs e) {
            string eName = InputHelper.SetString(e.CommandName);
            int id = InputHelper.SetNumber(e.CommandArgument);
            switch (eName.Trim().ToLower()) {
                case "go":
                    Bind_DataEdit(id);
                    break;
                case "del":
                    Bind_Delete(id);
                    break;
            }
        }
    }
}